from django.db import models


class BUS(models.Model):
    STATUS_CHOICES = [('正常', '正常'), ('维修', '维修')]

    bus_id = models.CharField('车牌号', max_length=50, primary_key=True)
    brand = models.CharField('品牌', max_length=50,  default='未知')
    model = models.CharField('车型', max_length=50)
    capacity = models.IntegerField('核载人数')
    status = models.CharField('状态', max_length=10, choices=STATUS_CHOICES, default='正常')

    class Meta:
        verbose_name = '车辆'
        verbose_name_plural = '车辆管理'

    def __str__(self):
        return self.bus_id


class DRIVER(models.Model):
    STATUS_CHOICES = [('在岗', '在岗'), ('休假', '休假')]

    driver_id = models.CharField('工号', max_length=20, primary_key=True)
    name = models.CharField('姓名', max_length=20)
    license = models.CharField('驾驶类型', max_length=50)
    contact = models.CharField('联系方式', max_length=20)
    status = models.CharField('状态', max_length=10, choices=STATUS_CHOICES, default='在岗')

    class Meta:
        verbose_name = '司机'
        verbose_name_plural = '司机管理'

    def __str__(self):
        return self.name


class ROUTE(models.Model):
    route_id = models.CharField('线路ID', max_length=20, primary_key=True)
    name = models.CharField('线路名称', max_length=50)
    start_station = models.CharField('起点站', max_length=50)
    end_station = models.CharField('终点站', max_length=50)
    total_distance = models.FloatField('总里程(km)')
    estimated_time = models.DurationField('预计时长')

    class Meta:
        verbose_name = '线路'
        verbose_name_plural = '线路管理'

    def __str__(self):
        return f"{self.name}({self.start_station}→{self.end_station})"


class STATION(models.Model):
    station_id = models.AutoField('站点ID', primary_key=True)
    route = models.ForeignKey(ROUTE, on_delete=models.CASCADE, verbose_name='所属线路', related_name='stations')
    name = models.CharField('站点名称', max_length=50)
    sequence = models.IntegerField('站点顺序')
    distance_from_start = models.FloatField('距起点距离(km)')

    class Meta:
        verbose_name = '站点'
        verbose_name_plural = '站点管理'
        # 确保同一线路中站点顺序唯一
        unique_together = ('route', 'sequence')

    def __str__(self):
        return f"{self.sequence}.{self.name}"


class SCHEDULE(models.Model):
    schedule_id = models.AutoField('班次ID', primary_key=True)
    route = models.ForeignKey(ROUTE, on_delete=models.CASCADE, verbose_name='线路')
    departure_time = models.DateTimeField('发车时间')
    bus = models.ForeignKey(BUS, on_delete=models.SET_NULL, null=True, verbose_name='车辆')
    driver = models.ForeignKey(DRIVER, on_delete=models.SET_NULL, null=True, verbose_name='司机')
    status = models.CharField('班次状态', max_length=20, default='计划中', choices=[
        ('计划中', '计划中'),
        ('进行中', '进行中'),
        ('已完成', '已完成'),
        ('已取消', '已取消')
    ])

    class Meta:
        verbose_name = '班次'
        verbose_name_plural = '班次管理'
        ordering = ['departure_time']

    def __str__(self):
        return f"{self.departure_time.strftime('%Y-%m-%d %H:%M')} | {self.route}"


class REPAIR(models.Model):
    repair_id = models.AutoField('维修ID', primary_key=True)
    bus = models.ForeignKey(BUS, on_delete=models.CASCADE, verbose_name='车辆')
    report_date = models.DateField('报修日期', auto_now_add=True)
    repair_date = models.DateField('修复日期', null=True, blank=True)
    reason = models.TextField('故障原因')
    cost = models.DecimalField('维修费用', max_digits=8, decimal_places=2, null=True, blank=True)
    repair_status = models.CharField('维修状态', max_length=20, choices=[
        ('待处理', '待处理'),
        ('维修中', '维修中'),
        ('已完成', '已完成')
    ], default='待处理')

    class Meta:
        verbose_name = '维修记录'
        verbose_name_plural = '维修管理'
        ordering = ['-report_date']

    def __str__(self):
        return f"{self.bus.bus_id} | {self.report_date}"